/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.editpart.factory;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;

import ro.pub.cs.se.moondance.editpart.AttributeEditPart;
import ro.pub.cs.se.moondance.editpart.ClassEditPart;
import ro.pub.cs.se.moondance.editpart.CompositeStateEditPart;
import ro.pub.cs.se.moondance.editpart.ConnectionEditPart;
import ro.pub.cs.se.moondance.editpart.DiagramEditPart;
import ro.pub.cs.se.moondance.editpart.EndStateEditPart;
import ro.pub.cs.se.moondance.editpart.OperationEditPart;
import ro.pub.cs.se.moondance.editpart.PackageEditPart;
import ro.pub.cs.se.moondance.editpart.RegionEditPart;
import ro.pub.cs.se.moondance.editpart.StartStateEditPart;
import ro.pub.cs.se.moondance.editpart.StateEditPart;
import ro.pub.cs.se.moondance.editpart.StateMachineEditPart;
import ro.pub.cs.se.moondance.model.ClassFunction;
import ro.pub.cs.se.moondance.model.ClassVariable;
import ro.pub.cs.se.moondance.model.Connection;
import ro.pub.cs.se.moondance.model.UMLBaseClass;
import ro.pub.cs.se.moondance.model.UMLCompositeState;
import ro.pub.cs.se.moondance.model.UMLDiagram;
import ro.pub.cs.se.moondance.model.UMLEndState;
import ro.pub.cs.se.moondance.model.UMLPackage;
import ro.pub.cs.se.moondance.model.UMLRegion;
import ro.pub.cs.se.moondance.model.UMLStartState;
import ro.pub.cs.se.moondance.model.UMLState;
import ro.pub.cs.se.moondance.model.UMLStateMachine;

/**
 * Factory that maps model elements to edit parts.
 */
public class UMLEditPartFactory implements EditPartFactory {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.gef.EditPartFactory#createEditPart(org.eclipse.gef.EditPart,
	 * java.lang.Object)
	 */
	public EditPart createEditPart(EditPart context, Object modelElement) {
		EditPart part = getPartForElement(modelElement);
		part.setModel(modelElement);
		return part;
	}

	/**
	 * Maps an object to an EditPart.
	 */
	public EditPart getPartForElement(Object modelElement) {
		if (modelElement instanceof UMLDiagram) {
			return new DiagramEditPart();
		}
		if (modelElement instanceof UMLBaseClass) {
			return new ClassEditPart();
		}
		if (modelElement instanceof UMLPackage) {
			return new PackageEditPart();
		}
		if (modelElement instanceof ClassVariable) {
			return new AttributeEditPart();
		}
		if (modelElement instanceof ClassFunction) {
			return new OperationEditPart();
		}
		if (modelElement instanceof Connection) {
			return new ConnectionEditPart();
		}
		if (modelElement instanceof UMLStartState) {
			return new StartStateEditPart();
		}
		if (modelElement instanceof UMLEndState) {
			return new EndStateEditPart();
		}
		if (modelElement instanceof UMLState) {
			return new StateEditPart();
		}
		if (modelElement instanceof UMLRegion) {
			return new RegionEditPart();
		}
		if (modelElement instanceof UMLCompositeState) {
			return new CompositeStateEditPart();
		}
		if (modelElement instanceof UMLStateMachine) {
			return new StateMachineEditPart();
		}
		throw new RuntimeException("Can't create part for model element: "
				+ ((modelElement != null) ? modelElement.getClass().getName()
						: "null"));
	}
}
